package com.itheima.manage.controller;

import com.itheima.commons.vo.backstage.AdminVo;
import com.itheima.manage.config.NoAuthorization;
import com.itheima.manage.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/system/users")
public class AdminController {
    @Autowired
    private AdminService adminService;

    //登录
    @NoAuthorization
    @PostMapping("/login")
    public ResponseEntity adminLogin(@RequestBody Map<String, String> map) {
        //获取map中的数据
        String username = map.get("username");
        String password = map.get("password");
        String verificationCode = map.get("verificationCode");
        String uuid = map.get("uuid");
        //验证账号密码是否正确
        adminService.login(username, password);
        //验证验证码是否正确
        adminService.checkCode(uuid, verificationCode);
        //生成token
        HashMap<String, Object> tokenMap = adminService.creatToken(username);

        return ResponseEntity.ok(tokenMap);
    }

    //生成图片二维码
    @NoAuthorization
    @GetMapping("/verification")
    public ResponseEntity creatVerification(@RequestParam String uuid) {
//        System.out.println(uuid);
        byte[] bytes = adminService.creatVerification(uuid);
        return ResponseEntity.ok(bytes);
    }

    //用户基本信息
    @PostMapping("/profile")
    public ResponseEntity profile() {
        AdminVo adminVo = adminService.getAdmin();
        return ResponseEntity.ok(adminVo);
    }

    //退出
    @PostMapping("/logout")
    public ResponseEntity signOut() {
        return ResponseEntity.ok(null);
    }
}
